import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {
   public static void main(String[] args) {
      try {
         new Client();
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   private Socket serverSocket;
   private BufferedReader in;
   private PrintWriter out;
   private String name;
   
   public Client() throws UnknownHostException, IOException {
      serverSocket = new Socket("172.25.200.78", 4444);
      out = new PrintWriter(serverSocket.getOutputStream(), true);
      
      String outLine;
      BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in));
      
      System.out.print("Enter your name > ");
      while((outLine = stdIn.readLine()) != null) {
         name = outLine;
         out.println(name); //sending server the name
         System.out.println("Speak, " + name + "!");
         break;
      }
      in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
      
      new InputWindow(name, out);
      new OutThread(stdIn, out, name).start();
      new InThread(in).start();
   }
}
